﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LuaInterface;
using System;
using System.Runtime.InteropServices;

public class systemfunction
{
    [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
    private static int send(IntPtr luaState)
    {
        int headLen = 0;
        int bodyLen = 0;
        IntPtr headData = LuaDLL.lua_tolstring(luaState, 1, out headLen);
        IntPtr bodyData = LuaDLL.lua_tolstring(luaState, 2, out bodyLen);
        byte[] buffer = new byte[headLen + bodyLen + 2];  //dwBufSize数据长度
        Marshal.Copy(headData, buffer, 2, headLen);
        Marshal.Copy(bodyData, buffer, 2 + headLen, bodyLen);
        byte[] lenBuff = BitConverter.GetBytes(((short)headLen));
        Array.Reverse(lenBuff);
        Array.Copy(lenBuff, buffer, 2);

        app.send(buffer);
        return 0;
    }

    [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
    private static int trace(IntPtr luaState)
    {
        string text = LuaDLL.lua_tostring(luaState, 1);
        Debug.Log(text);
        return 0;
    }
    [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
    private static int getPbcPath(IntPtr luaState)
    {
        LuaDLL.lua_pushstring(luaState, app.AppPersistentPath.getPbcPath());
        return 1;
    }
    public static void registerFunc()
    {
        List<LuaMethod> systemLib = new List<LuaMethod>();
        systemLib.Add(new LuaMethod("trace", systemfunction.trace));
        systemLib.Add(new LuaMethod("getPbcPath", systemfunction.getPbcPath));
        systemLib.Add(new LuaMethod("send", systemfunction.send));
        LuaHelper.getLuaModule().registerLib("System", systemLib.ToArray());
    }
}
